<?php

declare(strict_types=1);

namespace app\middleware;

use app\Request;
use Hashids\Hashids;

class CheckArticleId
{
  use \liliuwei\think\Jump;

  /**
   * 拒绝请求
   * Class AdminCkeckRole
   * @package appx\middleware
   */
  public function handle(Request $request, \Closure $next)
  {
    $akey = $request->param('akey');
    if ($akey && preg_match('/^[_0-9a-zA-Z]{7}$/', $akey)) {
      $hashids = new Hashids('soyulsmida', 7);
      $result = $hashids->decode($akey);
      $articleId =  count($result) ? $result[0] : false;
      if ($articleId) {
        $request->articleId = $articleId;
      } else {
        if (isset($_GET['ajax']))
          return json(['status' => 404, 'msg' => '404']);
        $this->redirect('/404');
      }
    }

    return $next($request);
  }
  ######################################################################################################################


}
